const express = require('express')
const fs = require('fs')
const cors = require('cors')
const bodyParser = require('body-parser')
const path = require('path')
const { send } = require('process')

//使用cors跨域
const app = express()
app.use(cors())
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

//登录与注册

//登录GET请求
app.get('/login',(req,res)=>{
    console.log(req.query)
    const file = JSON.parse(fs.readFileSync('../db/users.json','utf-8'))
    console.log(file);
    const query = req.query
    const user = query.user
    const psw = query.psw
    const result = file.find(i => {
      return i.user === user && i.psw === psw 
    })
    console.log(result);
    if(!result)
     res.send({
      code : 0
    })
    else{
      res.send({
        code : 1
      })
    }
})

//post请求   注册请求
app.post('/regist',(req,res)=>{
    console.log(req.body)
    const file = JSON.parse(fs.readFileSync('../db/users.json','utf-8'))
    console.log(file);
    req.body.id = file.length ? file[file.length - 1].id - 0 + 1 : 1
    const us = req.body
    console.log(us)
    file.push(us)
    fs.writeFileSync('../db/users.json',JSON.stringify(file))
    res.send({
      code : 1,
      message : '注册成功'
    })
})

// 商品分类请求  拿到所有东西
app.get('/goodslist',(req,res) => {
  const str = fs.readFileSync('../db/goodslist.json')
  res.send(str)
})
// 宠物
app.get('/chongwu',(req,res) => {
  const str = fs.readFileSync('../db/chongwu.json')
  res.send(str)
})
app.get('/geren',(req,res) =>{
  const str = fs.readFileSync('../db/geren.json')
  res.send(str)
})
app.get('/shuma',(req,res) => {
  const str = fs.readFileSync('../db/shuma.json')
  res.send(str)
})
app.get('/meirong',(req,res) => {
  const str = fs.readFileSync('../db/meirong.json')
  res.send(str)
})
app.get('/yingyang',(req,res) => {
  const str = fs.readFileSync('../db/yingyang.json')
  res.send(str)
})
app.get('/yiwu',(req,res) => {
  const str = fs.readFileSync('../db/yiwu.json')
  res.send(str)
})
app.get('/xiezi',(req,res) => {
  const str = fs.readFileSync('../db/xiezi.json')
  res.send(str)
})

// 排序接口
// 升序函数
function smallSort(arr) {
  return arr.sort((a,b) => {
    return a.content.actualCurrentPrice - b.content.actualCurrentPrice
   })
}
// 降序函数
function bigSort(arr) {
  return arr.sort((a,b) => {
    return b.content.actualCurrentPrice - a.content.actualCurrentPrice
   })
}

// 全部分类 升序 smallsort  降序 bigsort
let arr = JSON.parse(fs.readFileSync('../db/goodslist.json')).list 
// 升序
const goodS = smallSort(arr)
app.get('/sortsmallgoodslist',(req,res) => {
  res.send(goodS)
})
// 降序
const goodslist = bigSort(arr)
app.get('/sortbiggoodslist',(req,res) => {
  res.send(goodslist)
})
// 宠物馆分类
let brr = JSON.parse(fs.readFileSync('../db/chongwu.json')).list 
const chongwu = smallSort(brr)
app.get('/sortsmallchongwu',(req,res) => {
  res.send(chongwu)
})
const chongwub = bigSort(brr)
app.get('/sortbigchongwu',(req,res) => {
  res.send(chongwub)
})
// 数码家电
let crr = JSON.parse(fs.readFileSync('../db/shuma.json')).list 
const shuma = smallSort(crr)
app.get('/sortsmallshuma',(req,res) => {
  res.send(shuma)
})
const shumab = bigSort(crr)
app.get('/sortbigshuma',(req,res) => {
  res.send(shumab)
})
// 个人洗护
let drr = JSON.parse(fs.readFileSync('../db/geren.json')).list 
const geren = smallSort(drr)
app.get('/sortsmallgeren',(req,res) => {
  res.send(geren)
})
const gerenb = bigSort(drr)
app.get('/sortbiggeren',(req,res) => {
  res.send(gerenb)
})
// 美容
let err = JSON.parse(fs.readFileSync('../db/meirong.json')).list 
const meirong = smallSort(err)
app.get('/sortsmallmeirong',(req,res) => {
  res.send(meirong)
})
const meirongb = bigSort(err)
app.get('/sortbigmeirong',(req,res) => {
  res.send(meirongb)
})
// 营养套餐
let frr = JSON.parse(fs.readFileSync('../db/yingyang.json')).list 
const yingyang = smallSort(frr)
app.get('/sortsmallyingyang',(req,res) => {
  res.send(yingyang)
})
const yingyangb = bigSort(frr)
app.get('/sortbigyingyang',(req,res) => {
  res.send(yingyangb)
})
// 衣物
let grr = JSON.parse(fs.readFileSync('../db/yiwu.json')).list 
const yiwu = smallSort(grr)
app.get('/sortsmallyiwu',(req,res) => {
  res.send(yiwu)
})
const yiwub = bigSort(grr)
app.get('/sortbigyiwu',(req,res) => {
  res.send(yiwub)
})
// 鞋子
let hrr = JSON.parse(fs.readFileSync('../db/xiezi.json')).list 
const xiezi = smallSort(hrr)
app.get('/sortsmallxiezi',(req,res) => {
  res.send(xiezi)
})
const xiezib = bigSort(hrr)
app.get('/sortbigxiezi',(req,res) => {
  res.send(xiezib)
})




//升序




//启动服务器
const server = app.listen(8080,'localhost', function () {
  // const host = server.address().address
  const port = server.address().port
  console.log(`访问地址为 http://localhost:${port}`);
})







